/**
 * Markdown格式化工具
 * 用于将Markdown文本转换为HTML
 */

/**
 * 简单的Markdown转HTML函数
 * @param {String} markdown Markdown文本
 * @return {String} HTML文本
 */
export function formatMarkdown(markdown) {
  if (!markdown) return '';

  // 移除HTML标签转义，因为rich-text组件会自动处理HTML标签
  function processText(text) {
    return text.trim();
  }

  // 处理代码块 ```code```
  markdown = markdown.replace(/```([\s\S]*?)```/g, (match, code) => {
    return `<pre style="background-color: #f6f8fa; padding: 16px; border-radius: 6px; overflow-x: auto;"><code style="color: #24292e; font-family: monospace;">${processText(code)}</code></pre>`;
  });

  // 处理行内代码 `code`
  markdown = markdown.replace(/`([^`]+)`/g, (match, code) => {
    return `<code style="background-color: #f6f8fa; padding: 2px 4px; border-radius: 3px; font-family: monospace;">${processText(code)}</code>`;
  });

  // 处理标题 ## Heading
  markdown = markdown.replace(/^# (.*$)/gm, (match, content) => `<h1 style="font-size: 2em; margin: 0.67em 0;">${processText(content)}</h1>`);
  markdown = markdown.replace(/^## (.*$)/gm, (match, content) => `<h2 style="font-size: 1.5em; margin: 0.83em 0;">${processText(content)}</h2>`);
  markdown = markdown.replace(/^### (.*$)/gm, (match, content) => `<h3 style="font-size: 1.17em; margin: 1em 0;">${processText(content)}</h3>`);
  markdown = markdown.replace(/^#### (.*$)/gm, (match, content) => `<h4 style="font-size: 1em; margin: 1.33em 0;">${processText(content)}</h4>`);

  // 处理粗体 **text**
  markdown = markdown.replace(/\*\*(.*?)\*\*/g, (match, content) => `<strong style="font-weight: 600;">${processText(content)}</strong>`);

  // 处理斜体 *text*
  markdown = markdown.replace(/\*(.*?)\*/g, (match, content) => `<em style="font-style: italic;">${processText(content)}</em>`);

  // 处理链接 [text](url)
  markdown = markdown.replace(/\[(.*?)\]\((.*?)\)/g, (match, text, url) => {
    return `<a href="${url}" style="color: #0366d6; text-decoration: none;">${processText(text)}</a>`;
  });

  // 处理图片 ![alt](url)
  markdown = markdown.replace(/!\[(.*?)\]\((.*?)\)/g, (match, alt, url) => {
    return `<img src="${url}" alt="${processText(alt)}" style="max-width: 100%; height: auto;">`;
  });

  // 处理无序列表 - item
  markdown = markdown.replace(/^\s*-\s*(.*$)/gm, (match, content) => `<li style="margin: 4px 0;">${processText(content)}</li>`);
  markdown = markdown.replace(/(<li.*<\/li>)/s, '<ul style="padding-left: 20px; margin: 16px 0;">$1</ul>');

  // 处理有序列表 1. item
  markdown = markdown.replace(/^\s*\d+\.\s*(.*$)/gm, (match, content) => `<li style="margin: 4px 0;">${processText(content)}</li>`);
  markdown = markdown.replace(/(<li.*<\/li>)/s, '<ol style="padding-left: 20px; margin: 16px 0;">$1</ol>');

  // 处理段落
  markdown = markdown.replace(/^(?!<[a-z]).+$/gm, (match) => `<p style="margin: 16px 0; line-height: 1.6;">${processText(match)}</p>`);

  // 处理换行
  markdown = markdown.replace(/\n/g, '<br>');

  return markdown;
}

/**
 * 将Markdown转换为小程序rich-text支持的nodes
 * @param {String} markdown Markdown文本
 * @return {String} 适合rich-text的HTML
 */
export function markdownToNodes(markdown) {
  return formatMarkdown(markdown);
}